package com.hegongda.okcmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hegongda.okcmall.product.entity.AttrAttrgroupRelationEntity;
import com.hegongda.okcmall.product.entity.AttrEntity;
import com.hegongda.okcmall.product.service.CategoryService;
import com.hegongda.okcmall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hegongda.okcmall.product.entity.AttrGroupEntity;
import com.hegongda.okcmall.product.service.AttrGroupService;
import com.hegongda.common.utils.PageUtils;
import com.hegongda.common.utils.R;



/**
 * 属性分组
 *
 * @author Liuweidong
 * @email Liuweidong@gmail.com
 * @date 2021-01-31 17:57:14
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;


    // product/attrgroup/4/attr/relation?t=1613138206730  获取分组下的属性
    @GetMapping("{groupId}/attr/relation")
    public R getAttrRelation(@PathVariable("groupId") String groupId){
        List<AttrEntity> list =  attrGroupService.getAttrRelation(groupId);
        return R.ok().put("data", list);
    }

    // /product/attrgroup/4/noattr/relation  分组进行关联属性
    @GetMapping("{groupId}/noattr/relation")
    public R getNoAttrRelation(@RequestParam Map<String, Object> params,
                               @PathVariable("groupId") Long groupId){
        PageUtils page = attrGroupService.getNoAttrRelation(params,groupId);
        return R.ok().put("page", page);
    }

    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrGroupEntities){
        attrGroupService.saveBatchRelation(attrGroupEntities);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Integer catelogId ){
        PageUtils page = attrGroupService.query(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long []categoryIds = categoryService.getCategoryPath(attrGroup.getCatelogId());
        attrGroup.setCategoryIds(categoryIds);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除  attr/relation/delete
     */
    @PostMapping("attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> entities){
        attrGroupService.deleteAttrRelation(entities);
        return R.ok();
    }
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

    // http://localhost:88/api/product/attrgroup/225/withattr?t=1613212758236
    // 获取分类下所有分组以及其分组属性
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") String catelogId){
        List<AttrGroupWithAttrsVo> dataList = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data", dataList);
    }

}
